#include <Windows.h>
#include <stdio.h>
#include "cd_stack_test.h"
#include "cd_stack.h"
#include "cd_allocate.h"

CD_NAMESPACE_BEGIN

int	alloc_test(c_stack*	stack)
{
	struct p_node 
	{
		p_node*	next;
	};
	__int64	i	=	0;
	LARGE_INTEGER	fre;
	LARGE_INTEGER	cnt, prevcnt;
	//LONGLONG		QPart1,QPart2;
	//double			dfMinus, dfFreq, dfTim; 
	long	sz	=	0;

	QueryPerformanceFrequency(&fre);
	QueryPerformanceCounter(&prevcnt);

	p_node*	pv	=	0,*pv1	=	0,*pvt	=	0;
	while(i	<	0xFFFFF)
	{
		const	long	sz	=	8;
		pv	=	(p_node*)stack->allocate(sz);

		if(pv){
			if(!pvt)	pvt	=	pv;	
			pv->next	=	pv1;
			pv1			=	pv;
			++i;
		}
		else	continue;

		if(i%	0xFFFFF	==	0)
		{
			QueryPerformanceCounter(&cnt);
			printf("\n{ALLOC %d, spend:%lf seconds}\n",0xFFFFF, (double)(cnt.QuadPart	-	prevcnt.QuadPart)	/	fre.QuadPart);
			alloc::m_pool[0].print();
			prevcnt	=	cnt;
			break;
		}

	}
	p_node*	pv2	=	0;
	while(pv1)
	{
		if(!pv1->next	&&	pv1	!=	pvt)
		{
			//			alloc::deallocate(pv1,	8);
			break;
		}
		++sz;

		pv2	=	pv1;
		pv1	=	pv1->next;
		pv2->next	=	0;
		stack->deallocate(pv2,	8);
	}
	printf("real sz:%d\n", sz);
	return	0;
}

int	stack_push_test(c_stack*	stack)
{

	stack->push_create(1);
	stack->pop_clean(1);
	stack->push_frame();
	stack->call();
	stack->ret();
	return	0;
}

int	stack_test()
{
	c_stack	stack;
	
	alloc_test(&stack);
	stack_push_test(&stack);
	return	0;
}


CD_NAMESPACE_END